package com.longxi.tianjintianbao.ui.presenter.maintain;

import android.content.Intent;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.BaseResponse;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.model.completeEnter.EnterEditText;
import com.longxi.tianjintianbao.model.maintain.Maintain;
import com.longxi.tianjintianbao.model.maintain.MaintainDetailed;
import com.longxi.tianjintianbao.ui.activity.common.CompleteEnterActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.LabelViewBinder;
import com.longxi.tianjintianbao.ui.adapter.multitype.maintain.MaintainDetailViewBinder;
import com.longxi.tianjintianbao.ui.adapter.multitype.maintain.MaintainDetailedViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.presenter.common.CompleteEnterAtPresenter;
import com.longxi.tianjintianbao.ui.view.maintain.IMaintainDetailAtView;
import com.longxi.tianjintianbao.utils.MyUtils;

import org.json.JSONException;
import org.json.JSONObject;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MaintainDetailAtPresenter extends BasePresenter<IMaintainDetailAtView> {
    private Maintain maintain = null;

    private MultiTypeAdapter adapter;
    private Items items;

    public MaintainDetailAtPresenter(BaseActivity context) {
        super(context);
    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            /* 注册类型和 View 的对应关系 */
            adapter.register(Maintain.class, new MaintainDetailViewBinder());
            adapter.register(String.class, new LabelViewBinder());
            adapter.register(MaintainDetailed.class, new MaintainDetailedViewBinder());
            getView().getRvContent().setAdapter(adapter);
        }
    }

    public void setDate() {
        Intent intent = mContext.getIntent();
        maintain = (Maintain) intent.getSerializableExtra(AppConst.MAINTAIN);
        if(maintain == null){
            MyUtils.showToast("数据异常");
            mContext.showNoData();
            return;
        }

        items = new Items();
        items.add(maintain);
        items.add("保养标准");
        items.addAll(maintain.getDetailed());
        adapter.setItems(items);
        adapter.notifyDataSetChanged();
        mContext.hideLoading();
    }

    public void toCompleteEnterActivity() {
        Intent intent = new Intent(mContext, CompleteEnterActivity.class);
        intent.putExtra(CompleteEnterAtPresenter.TITLE, "添加保养记录");
        EnterEditText enterEditText = new EnterEditText("请输入保养说明", "");
        intent.putExtra(CompleteEnterAtPresenter.ENTER_EDITTEXT, enterEditText);
        mContext.startActivityForResult(intent, AppConst.IntentRequstCode.ACTIVITY_COMPLETE_ENTER);
    }

    public void onActivityResult(int requestCode, Intent data) {
        switch (requestCode) {
            case AppConst.IntentRequstCode.ACTIVITY_COMPLETE_ENTER:
                if(data == null){
                    return;
                }
                saveEquipmentMaintain(data.getStringExtra(CompleteEnterAtPresenter.ENTEREDITTEXT));
                break;
        }
    }

    public void saveEquipmentMaintain(String explain){
        if("".equals(explain)){
            MyUtils.showToast("未填写保养说明");
            return;
        }
        mContext.showWaitingDialog(MyUtils.getString(R.string.upload));
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("maintainid", maintain.getMaintainid());
            object.put("explain", explain);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ApiRetrofit.getInstance().saveEquipmentMaintain(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<BaseResponse>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                mContext.hideWaitingDialog();
                mContext.hideWaitingDialog();
                MyUtils.showToast("保养记录上传成功");
            }
        });
    }
}
